#include "UIMessage.h"
#include "dlg.h"
#include "timeSetWinDlg.h"
#include "LcdControl.h"
#include "GUI.h"
#include "GUI_LCD.h"
#include "system_time.h"

uint8_t tmstDateAddChar(char Ch);

typedef struct WG_tmstDate{
  Widget Widget;
  char Year[5];
  char Month[3];
  char Day[3];
  uint8_t Activate;
}WG_tmstDate;

WG_tmstDate _tmst_date = {0};
Widget *tmstDate;

static void tmstDateCallback(WM_Message *pMsg)
{
  uint8_t Length;
  switch(pMsg->msgType)
  {
    case WM_KEY:
      if(((WM_KEY_INFO*)pMsg->data_p)->Pressed == KEY_UP)
      { 
        switch(((WM_KEY_INFO*)pMsg->data_p)->Key)
        {
          case GUI_KEY_DEL:
          case GUI_KEY_STAR:
            if(tmstDateGetUnderNum())
            {
              GUI_SetCharUnderLine(_tmst_date.Year, TMST_YEAR_LENGTH);
              GUI_SetCharUnderLine(_tmst_date.Month, TMST_MONTH_LENGTH);
              GUI_SetCharUnderLine(_tmst_date.Day, TMST_DAY_LENGTH);
              WG_Paint(tmstDate);
              AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            }
            else
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
            }
            break;
          case GUI_KEY_ENTER:
          {
            TMST_DATE Date;
            TMST_TIME Time;
            _tmst_date.Activate = TMST_EDIT_UNACTIVATE;
            WG_Paint(tmstDate);
            tmstDateGetVal(&Date);
            tmstTimeGetVal(&Time);
            SysTime_Set(Date.Year, Date.Month, Date.Day, 
                Time.Hour, Time.Minute, Time.Second);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
          }
            break;
          case GUI_KEY_UP:
          case GUI_KEY_DOWN:
            WG_SetFocus(tmstTime);
            WG_Paint(tmstTime);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_0_CALLSHIP:
            Length = tmstDateAddChar('0');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_1_GROUP:
            Length = tmstDateAddChar('1');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_2_DIGIT:
            Length = tmstDateAddChar('2');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_3_SIMULATE:
            Length = tmstDateAddChar('3');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_4_LOG:
            Length = tmstDateAddChar('4');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_5_DIRECTION:
            Length = tmstDateAddChar('5');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_6_REMEMBER:       
            Length = tmstDateAddChar('6');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_7_SIGN: 
           Length = tmstDateAddChar('7');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_8_WEATHER:        
            Length = tmstDateAddChar('8');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          case GUI_KEY_9_SCAN:         
            Length = tmstDateAddChar('9');
            if(!Length)
            {
              AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
              break;
            }
            if(Length == 8)
            {
              if(tmstTimeGetUnderNum())
              {
                WG_SetFocus(tmstTime);
                WG_Paint(tmstTime);
              }
            }
            WG_Paint(tmstDate);
            AudioPlay(AUDIO_TYPE_KEY_NORMAL);
            break;
          default:
            AudioPlay(AUDIO_TYPE_KEY_UNNORMAL);
            break;
        }
      }
      break;
    case WM_CREATE:
      GUI_SetCharUnderLine(_tmst_date.Year, TMST_YEAR_LENGTH);
      GUI_SetCharUnderLine(_tmst_date.Month, TMST_MONTH_LENGTH);
      GUI_SetCharUnderLine(_tmst_date.Day, TMST_DAY_LENGTH);
      _tmst_date.Activate = TMST_EDIT_UNACTIVATE;
      break;
    case WM_FOCUS:
      if(pMsg->data_v)
      {
        GUI_SetCharUnderLine(_tmst_date.Year, TMST_YEAR_LENGTH);
        GUI_SetCharUnderLine(_tmst_date.Month, TMST_MONTH_LENGTH);
        GUI_SetCharUnderLine(_tmst_date.Day, TMST_DAY_LENGTH);
        _tmst_date.Activate = TMST_EDIT_ACTIVATE;
      }
      else
      {
        _tmst_date.Activate = TMST_EDIT_UNACTIVATE;
      }
      break;
    case WM_PAINT:
      if(_tmst_date.Widget.States >= WG_VISIBLE && pMsg->data_v)
      {
        uint8_t flash = 1;
        GUI_LCDSetS(LCD_S49, 1);
        GUI_LCDSetS(LCD_S41, 1);
        GUI_LCDSetS(LCD_S42, 1);
        
        if(_tmst_date.Activate == TMST_EDIT_UNACTIVATE) flash = 0;
        
        if(flash && _tmst_date.Year[0] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM4, _tmst_date.Year[0], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM4, _tmst_date.Year[0], 0);
        }
        
        if(flash && _tmst_date.Year[1] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM5, _tmst_date.Year[1], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM5, _tmst_date.Year[1], 0);
        }
        
        if(flash && _tmst_date.Year[2] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM6, _tmst_date.Year[2], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM6, _tmst_date.Year[2], 0);
        }
        
        if(flash && _tmst_date.Year[3] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM7, _tmst_date.Year[3], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM7, _tmst_date.Year[3], 0);
        }
        
        GUI_LCDSetS(LCD_S50, 1);
        GUI_LCDSetS(LCD_S40, 1);
        GUI_LCDSetS(LCD_S39, 1);
        
        if(flash && _tmst_date.Month[0] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM9, _tmst_date.Month[0], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM9, _tmst_date.Month[0], 0);
        }
        
        if(flash && _tmst_date.Month[1] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM10, _tmst_date.Month[1], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM10, _tmst_date.Month[1], 0);
        }
        
        GUI_LCDSetS(LCD_S53, 1);
        GUI_LCDSetS(LCD_S37, 1);
        GUI_LCDSetS(LCD_S36, 1);
        
        if(flash && _tmst_date.Day[0] == '_')
        {
          GUI_LCDSetNumChar(LCD_NUM12, _tmst_date.Day[0], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM12, _tmst_date.Day[0], 0);
        }
        
        if(flash)
        {
          GUI_LCDSetNumChar(LCD_NUM13, _tmst_date.Day[1], 1);
          flash = 0;
        }
        else
        {
          GUI_LCDSetNumChar(LCD_NUM13, _tmst_date.Day[1], 0);
        }
      }
      else
      {
        GUI_LCDDisableNumFlash();
        GUI_LCDSetS(LCD_S49, 0);
        GUI_LCDSetS(LCD_S41, 0);
        GUI_LCDSetS(LCD_S42, 0);
        
        GUI_LCDSetNumChar(LCD_NUM4, LCD_DISPNULL, 0);
        GUI_LCDSetNumChar(LCD_NUM5, LCD_DISPNULL, 0);
        GUI_LCDSetNumChar(LCD_NUM6, LCD_DISPNULL, 0);
        GUI_LCDSetNumChar(LCD_NUM7, LCD_DISPNULL, 0);
        
        GUI_LCDSetS(LCD_S50, 0);
        GUI_LCDSetS(LCD_S40, 0);
        GUI_LCDSetS(LCD_S39, 0);
        
        GUI_LCDSetNumChar(LCD_NUM9, LCD_DISPNULL, 0);
        GUI_LCDSetNumChar(LCD_NUM10, LCD_DISPNULL, 0);
        
        GUI_LCDSetS(LCD_S53, 0);
        GUI_LCDSetS(LCD_S37, 0);
        GUI_LCDSetS(LCD_S36, 0);
        
        GUI_LCDSetNumChar(LCD_NUM12,LCD_DISPNULL, 0);
        GUI_LCDSetNumChar(LCD_NUM13,LCD_DISPNULL, 0);
        
      }
      break;
  }
}

Widget* tmstDateCreate(void)
{
  WG_Create((Widget*)&_tmst_date, &timeSetWin, tmstDateCallback);
  tmstDate = (Widget*)&_tmst_date;
  return tmstDate;
}

uint8_t tmstDateAddChar(char Ch)
{
  uint8_t length = 0;
  GUI_CharToUint16(_tmst_date.Year, &length);
  if(length)
  {
    return GUI_AddAChar(_tmst_date.Year, Ch, TMST_YEAR_LENGTH);
  }
  length = 0;
  GUI_CharToUint8(_tmst_date.Month, &length);
  if(length)
  {
    return GUI_AddAChar(_tmst_date.Month, Ch, TMST_MONTH_LENGTH) + 4;
  }
  length = 0;
  GUI_CharToUint8(_tmst_date.Day, &length);
  if(length)
  {
    return GUI_AddAChar(_tmst_date.Day, Ch, TMST_DAY_LENGTH) + 6;
  }
  return 0;
}

uint8_t tmstDateGetUnderNum(void)
{
  uint8_t length = 0, sum = 0;
  GUI_CharToUint16(_tmst_date.Year, &length);
  sum += length;
  GUI_CharToUint8(_tmst_date.Month, &length);
  sum += length;
  GUI_CharToUint8(_tmst_date.Day, &length);
  sum += length;
  return sum;
}

void tmstDateGetVal(TMST_DATE *data)
{
  data->Year = GUI_CharToUint16(_tmst_date.Year, 0);
  data->Month = GUI_CharToUint8(_tmst_date.Month, 0);
  data->Day = GUI_CharToUint8(_tmst_date.Day, 0);
}
